name: Integration Tests

on:
  pull_request:
    paths:
      - src/**
      - integration/example-library/**
      - noxfile.py
      - pyproject.toml
      - .github/workflows/integration_tests.yml
      - .github/workflows/resources/constraints.txt
  push:
    branches: [main]
    paths:
      - src/**
      - integration/example-library/**
      - noxfile.py
      - pyproject.toml
      - .github/workflows/integration_tests.yml
      - .github/workflows/resources/constraints.txt
  workflow_dispatch:
    inputs: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  validate:
    strategy:
      matrix:
        include:
        - { integration_test: "meltano-basics", needs_postgres: false}
        - { integration_test: "meltano-run", needs_postgres: true}
        - { integration_test: "meltano-objects", needs_postgres: false}
        - { integration_test: "meltano-migrations", needs_postgres: false}
        - { integration_test: "meltano-state-s3", needs_postgres: false}
        - { integration_test: "meltano-state-local", needs_postgres: false}
        - { integration_test: "meltano-config", needs_postgres: false}
        - { integration_test: "meltano-annotations", needs_postgres: false}
        - { integration_test: "meltano-manifest", needs_postgres: false}
        - { integration_test: "meltano-run-merge-states", needs_postgres: false}
        - { integration_test: "meltano-expand-envvars-in-array", needs_postgres: false}
      fail-fast: false

    runs-on: ubuntu-latest
    steps:
    - name: Check out the repository
      uses: actions/checkout@v4.1.4

    - name: Install Poetry
      env:
        PIP_CONSTRAINT: ${{ github.workspace }}/.github/workflows/resources/constraints.txt
      run: |
        pipx install poetry
        poetry --version

    - name: Setup Python 3.10
      uses: actions/setup-python@v5.1.0
      with:
        python-version: '3.10'
        architecture: x64
        cache: 'poetry'

    - name: Upgrade pip
      env:
        PIP_CONSTRAINT: ${{ github.workspace }}/.github/workflows/resources/constraints.txt
      run: |
        pip install pip
        pip --version

    - name: Install Dependencies
      run: |
        poetry env use "3.10"
        poetry install --extras "s3"

    - name: Start Postgres Container
      if: always() && (matrix.needs_postgres == true)
      run: >
        docker run -d
        -p "5432:5432"
        -e "POSTGRES_PASSWORD=postgres"
        -e "POSTGRES_DB=warehouse"
        --name postgres
        --health-cmd "pg_isready -d postgres -U postgres"
        --health-interval 10s
        --health-timeout 5s
        --health-retries 5
        postgres:11

    - name: Run integration tests
      run: |
        poetry env use "3.10"
        poetry run bash integration/validate.sh ${{ matrix.integration_test }}
